

* Open log
capture log close
log using "Data analysis\cbld-analysis02-fig05.log", replace text


* *******************************************************************************
* Fig. 5. Effect of voting threshold on winning coalition size by membership size
* *******************************************************************************

* Programme:	cbld-analysis02-fig05.do
* Project:		Council coalition building
* Author:		Frank Haege, Department of Politics and Administration, University of Limerick
* Contact:		frank.haege@ul.ie

* Description
*************
* This do-file generates a figure demonstrating the nonlinear relationship between 
* voting threshold and winning coalition size for different membership sizes. 
* For each membership size, a panel shows how the predicted median curve and interquartile
* range, the median winning coalition size based on the simulated data, and the effective
* voting threshold varies as a function of the formal voting threshold.


* Set up Stata
version 11
clear all
macro drop _all
set linesize 80
set more off

* Load data set
use "Experiments\Experiment01\cbld-model-121020-exp01.dta", clear


* Prepare data for non-linear quantile regression analysis in R
***************************************************************

* Generate consensus variable
generate consensus = (noblock/nostates)*100
label var consensus "Size of Winning Coalition (Percent)"

* Generate variables standardized between 0 and 1	
generate normconsensus = ((consensus - 50) * 2) / 100
label var normconsensus "nrmlzd: Size of Winning Coalition"
generate normvthresh = ((vthresh - 50) * 2) / 100
label var normvthresh "nrmlzd: Voting Threshold"

* Generate variable taking the logarithm of the standardized variables
generate logconsensus = log10(normconsensus)
label var logconsensus "log: Size of Winning Coalition"
tab logconsensus, m
generate logvthresh = log10(normvthresh)
label var logvthresh "log: Voting Threshold"
tab logvthresh


* Save data for non-linear quantile regression analysis in R and load results
*****************************************************************************

* Save data for model fitting in R
compress
saveold "Experiments\Experiment01\cbld-analysis02-fig05.dta", replace

* Load quantile regression estimation results from R
use "Experiments\\Experiment01\\cbld-analysis02-fig05-r.dta", clear

	
* Generate plot of non-linear affect of voting threshold on winning coalition size
**********************************************************************************

* Generate additional variables for plotting
generate x = _n/100 in 1/100
generate x100 = x*100/2+50

* Plot median prediction curve and simulated median winning coalition size against voting threshold for each membership size
foreach k of numlist 6 9 12 15 18 21 24 27 30 { 	
	* Save median winning coalition size in local for each combination of voting threshold and membership size
	foreach i of numlist 51 55 60 65 70 75 80 85 90 {
		qui sum consensus if vthresh == `i' & nostates == `k', d
		local median_`i' = r(p50)
	}
	* Extract exponent for median
	qui sum cfit2m if nostates == `k', d
	local bm = r(mean)
	local bms = string(round(`bm', .1))
	* Extract intercept for median
	qui sum ifit2m if nostates == `k', d
	local am = r(mean)
	local ams = string(round(`am', .01))
	* Extract exponent for lower quartile
	qui sum cfit2lq if nostates == `k', d
	local blq = r(mean)
	* Extract intercept for lower quartile
	qui sum ifit2lq if nostates == `k', d
	local alq = r(mean)
	* Extract exponent for upper quartile
	qui sum cfit2uq if nostates == `k', d
	local buq = r(mean)
	* Extract intercept for upper quartile
	qui sum ifit2uq if nostates == `k', d
	local auq = r(mean)
	* Generate prediction curve for median
	generate predm`k' = ((`am' + (1-`am')*(1-(1-x)^`bm'))*100)/2+50
	* Generate prediction curve for lower quartile
	generate predlq`k' = ((`alq' + (1-`alq')*(1-(1-x)^`blq'))*100)/2+50
	* Generate prediction curve for upper quartile
	generate preduq`k' = ((`auq' + (1-`auq')*(1-(1-x)^`buq'))*100)/2+50
	replace preduq`k' = 50 if preduq`k' < 50
	* Generate plot
	twoway (rarea preduq`k' predlq`k' x100, color(gs2)) /*
		*/ (function y = ((ceil((x/100)*`k'))/`k')*100, lcolor(gs4) range(51 100) ylabel(50 60:100)) /*
		*/ (line predm`k' x100, lcolor(white)) /*
		*/ (scatteri /*
		*/ `median_90' 90 /*
		*/ `median_85' 85 /*
		*/ `median_80' 80 /*
		*/ `median_75' 75 /*
		*/ `median_70' 70 /*
		*/ `median_65' 65 /*
		*/ `median_60' 60 /*
		*/ `median_55' 55 /*
		*/ `median_51' 51, mcolor(white) msymbol(Oh)) /*
		*/ , aspectratio(0.9)	/*
		*/  subtitle("`k'", box bexpand) /*
		*/ legend(order(3 1 4 2) label(1 "Inter-Quartile Range Fit") label(2 "Effective Voting Threshold") /*
		*/ label(3 "Median Regression Fit") label(4 "Observed Conditional Medians") /*
		*/ size(vsmall) symxsize(*0.4) symysize(*0.4) col(1) region(lcolor(black))) /*
		*/ saving("Data analysis\Graphs\cbld-analysis02-fig05-`k'", replace)
}

* Plot all median prediction curves for comparison
twoway (line predm6 x100, lcolor(white)  aspectratio(0.9) ylabel(50 60:100) xtitle("")) /*
	*/ (line predm9 x100, lcolor(white)) /*
	*/ (line predm12 x100, lcolor(white)) /*
	*/ (line predm15 x100, lcolor(white)) /*
	*/ (line predm18 x100, lcolor(white)) /*
	*/ (line predm21 x100, lcolor(white)) /*
	*/ (line predm24 x100, lcolor(white)) /*
	*/ (line predm27 x100, lcolor(white)) /*
	*/ (line predm30 x100, lcolor(white)) /*
	*/ , legend(off) subtitle("Comparison", box bexpand) /*
	*/ saving("Data analysis\Graphs\cbld-analysis02-fig05", replace)

* Drop redundant variables
drop x1 x100

* Combine all generated plots into a single graph
grc1leg /*
		*/ "Data analysis\Graphs\cbld-analysis02-fig05-6" /*
		*/ "Data analysis\Graphs\cbld-analysis02-fig05-9" /*
		*/ "Data analysis\Graphs\cbld-analysis02-fig05-12" /*
		*/ "Data analysis\Graphs\cbld-analysis02-fig05-15" /*
		*/ "Data analysis\Graphs\cbld-analysis02-fig05-18" /*
		*/ "Data analysis\Graphs\cbld-analysis02-fig05-21" /*
		*/ "Data analysis\Graphs\cbld-analysis02-fig05-24" /*
		*/ "Data analysis\Graphs\cbld-analysis02-fig05-27" /*
		*/ "Data analysis\Graphs\cbld-analysis02-fig05-30" /*
		*/ "Data analysis\Graphs\cbld-analysis02-fig05" /*
		*/ , cols(3) imargin(tiny) position(5) ring(0) ysize(10) xsize(4) /*
		*/ name(vteffect, replace) /*
		*/ l1title("Winning Coalition Size", size(small)) /*
		*/ b1title("Voting Threshold", size(small) bexpand justification(left) margin(l=6)) /*
		*/ legendfrom("Data analysis\Graphs\cbld-analysis02-fig05-6")
		
* Reshape and display graph
graph display vteffect, xsize(4.3) ysize(5)


log close
exit
